﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using condortravel.operaciones.common.Log;
using condortravel.operaciones.common;
using System.Collections.Specialized;
using CondorTravelOperacionesWeb.Source;
using Spring.Context;
using Spring.Context.Support;

namespace CondorTravelOperacionesWeb
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            LoadConfigurationCondorTOperaciones();
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            try
            {
                Exception ex = Server.GetLastError().InnerException;
                LogApp.write(ELogLevel.FATAL, string.Format("{0} - {1}", ex.Message, ex.StackTrace));
                //this.Response.Redirect(URLDirection

            }
            catch (Exception ex1)
            {
                LogApp.write(ELogLevel.FATAL, string.Format("{0} - {1}", ex1.Message, ex1.StackTrace));
            }


        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// Load information for Application
        /// </summary>
        private void LoadConfigurationCondorTOperaciones()
        {
            if (IntPtr.Size == 8)
            {
                LogApp.write(ELogLevel.FATAL, "Running as a 64 bit app");
            }
            else if (IntPtr.Size == 4)
            {
                LogApp.write(ELogLevel.FATAL, "Running as a 32 bit app");
            }
            //0. Load
            string[] names = ConfigurationManager.AppSettings.AllKeys;
            NameValueCollection appStgs = ConfigurationManager.AppSettings;

            int i = 0;
            for (i = 0; i <= appStgs.Count - 1; i++)
            {
                //Session.Add(names(i), appStgs(i))
                if (Application.Get(names[i]) == null)
                {
                    Application.Add(names[i], appStgs[i]);
                }
            }

            //1.Load AppSetting to Application
            Util.LoadAppSettingToApplication(this.Application);
            
            //2.Update Key of "SQLConnection" from before load .
            //ConfigurationSettings.AppSettings[AppStatic.REGISTRO_CLAVE_EVOLUTILITY] = Application[AppStatic.CONNECTION_REGEDIT_FFUSER].ToString();
        }
    }
}